//
//  DKTabBarController.m
//  BSBDJ
//
//  Created by 丁侃 on 16/4/23.
//  Copyright © 2016年 丁侃. All rights reserved.
//

#import "DKTabBarController.h"
#import "DKPbulishViewController.h"
#import "DKNewViewController.h"
#import "DKMeViewController.h"
#import "DKFriendViewController.h"
#import "DKEssenceViewController.h"

#import "DKTabBar.h"

@interface DKTabBarController ()

@end

@implementation DKTabBarController

- (void)viewDidLoad {
    [super viewDidLoad];

    //添加子控制器
    [self setupChildControllers];
    

    
    /*
        tabBar上有5个按钮,除了中间的按钮的控制器是modal出来的,其他都是通过tabBar来切换控制器,随意这里需要自定义一个中间的按钮,
     方式一:创建5个控制器,中间的为占位控制器,将我们自定义的按钮添加到中间
        这种方式的弊端,
            弊端一:多创建了一个占位的控制器,和一个占位的tabBarItem,如果在viewDidLoad中添加按钮,那么自定义按钮是被系统的item覆盖在上面,无法监听按钮的点击
            弊端二:所以只能在viewWillAppearance中添加按钮,这样就讲按钮添加在item之上, 但是viewWillAppearance 会调用多次,按钮不会多次创建添加,所以这里可以使用GCD的一次性代码/懒加载来解决
     
     方式二:自定义tabBar,重新布局按钮的位置
     
     */
}

-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    //系统的tabBar是只读的,无法通过赋值的方式来添加,只能通过KVC
    DKTabBar *tabBar =[[DKTabBar alloc]init];
    
    tabBar.tabBarBtnBlock = ^{
    
        DKPbulishViewController *publish = [[DKPbulishViewController alloc]init];
        [self presentViewController:publish animated:YES completion:^{
            
            dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                [publish dismissViewControllerAnimated:YES completion:nil];
            });
            
        }];
    };
    
    [self setValue:tabBar forKey:@"TabBar"];
}

-(void)click
{
    DKLog(@"--");
}


//统一设置tabBarItem的字体的属性
+(void)initialize
{
    //获取全局的tabBar,凡是方法/属性后面有UI_APPEARANCE_SELECTOR的都可以通过appearance来进行统一的设置
    UITabBarItem *item = [UITabBarItem appearance];
    
    NSDictionary *dicNormal = @{
                          NSFontAttributeName: [UIFont systemFontOfSize:14.0],
                          NSForegroundColorAttributeName:[UIColor grayColor]
                          };
    [item setTitleTextAttributes:dicNormal forState:UIControlStateNormal];
    
    NSDictionary *dicSel = @{
                             NSFontAttributeName: [UIFont systemFontOfSize:14.0],
                             NSForegroundColorAttributeName:[UIColor darkGrayColor]
                             };
    
    [item setTitleTextAttributes:dicSel forState:UIControlStateSelected];
}

//添加子控制器
-(void)setupChildControllers
{
    //精华
    DKEssenceViewController *essence = [[DKEssenceViewController alloc]init];
    [self setupOneChildController:essence withImageName:@"tabBar_essence_icon" withSelectedImageName:@"tabBar_essence_click_icon" withTabBarTitle:@"精华" withNavTitle:@"精华"];
    
    //新帖
    DKNewViewController *new = [[DKNewViewController alloc]init];
    [self setupOneChildController:new withImageName:@"tabBar_new_icon" withSelectedImageName:@"tabBar_new_click_icon" withTabBarTitle:@"关注" withNavTitle:@"关注"];
    //关注
    DKFriendViewController *friend = [[DKFriendViewController alloc]init];
    [self setupOneChildController:friend withImageName:@"tabBar_friendTrends_icon" withSelectedImageName:@"tabBar_friendTrends_click_icon" withTabBarTitle:@"关注" withNavTitle:@"关注"];
    
    //我的
    DKMeViewController *me = [[DKMeViewController alloc]init];
    [self setupOneChildController:me withImageName:@"tabBar_me_icon" withSelectedImageName:@"tabBar_me_click_icon" withTabBarTitle:@"我的" withNavTitle:@"我的"];

    
    
}

//添加一个自控制器
-(void)setupOneChildController:(UIViewController *) vc withImageName:(NSString *)imageName withSelectedImageName:(NSString *)selImageName withTabBarTitle:(NSString *)tabBartitle withNavTitle:(NSString *)navTitle
{
    //控制器的view是懒加载的,如果在这里就使用到了view,那么在添加控制器的时候就把view也创建了,所以不能再这里通过点语法来访问自控制器的view
    vc.navigationItem.title = navTitle;
    UINavigationController *nav = [[UINavigationController alloc]initWithRootViewController:vc];
    nav.tabBarItem.title = tabBartitle;
    
    //需要对图片是否存在进行判断,只有通过length才能准确判断
    if (imageName.length > 0) {
        nav.tabBarItem.image = [UIImage imageNamed:imageName];
    }
    if (selImageName.length > 0) {
        nav.tabBarItem.selectedImage = [UIImage imageNamed:selImageName];
    }
    
    [self addChildViewController:nav];
}


@end
